import { createRouter, createWebHashHistory } from 'vue-router';

// 引入主布局
import Admin from '~/layouts/admin.vue';

// 引入页面组件
import Index from '~/pages/index.vue';
import NotFound from '~/pages/404.vue';
import Login from '~/pages/login.vue';
import Chat from '~/pages/model/chat.vue';
import Draw from '~/pages/model/draw.vue';
import Knowledge from '~/pages/knowledge/knowledge.vue';
import Knowledgedetail from '~/pages/knowledge/knowledgedetail.vue';


const routes = [
  {
    path: '/',
    component: Admin,
    children: [
      {
        path: '/',
        component: Index,
        meta: {
          title: '首页', // 确保首页标题正确设置
        },
      },
      {
        path: '/chat',
        component: Chat,
        meta: {
          title: '对话',
        },
      },
      {
        path: '/draw',
        component: Draw,
        meta: {
          title: '绘图',
        },
      },
      {
        path: '/knowledge',
        component: Knowledge,
        meta: {
          title: '知识库',
        },
      },
      {
        path: '/knowledge/:id',
        component: Knowledgedetail,
        meta: {
          title: '知识库详情',
        },
      },
    ],
  },
  {
    path: '/login',
    component: Login,
    meta: {
      title: '登录页',
    },
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: NotFound,
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

// // 全局前置守卫
// router.beforeEach(async (to, from, next) => {
//   // 动态设置页面标题
//   if (to.path.startsWith('/knowledge/') && to.params.id) {
//     try {
//       const knowledgeName = await getKnowledgeNameById(to.params.id); // 假设有一个获取知识库名称的方法
//       to.meta.title = `知识库详情 - ${knowledgeName}`;
//     } catch (error) {
//       console.error('获取知识库名称失败:', error);
//       to.meta.title = '知识库详情';
//     }
//   }
//   document.title = to.meta.title || 'Vue3 Admin';
//   next();
// });



export default router;